package cn.bblocks.cacheTest.exceptions;

import lombok.extern.slf4j.Slf4j;

import org.springframework.orm.ObjectOptimisticLockingFailureException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.bblocks.cacheTest.constant.Constant;

import cn.bblocks.cacheTest.suport.dto.response.Response;

/**
 * @program: inte-kids
 * @author: captain.ma
 * @date: 2018-12-27
 * @since: 1.0.0.0
 */
@ControllerAdvice
@Slf4j
public class ExceptionHandle {

	  @ExceptionHandler(ObjectOptimisticLockingFailureException.class)
	    @ResponseBody
	    Response handleOptimisticLockingFailureException(ObjectOptimisticLockingFailureException ex) {
	    	//ex.printStackTrace();
	        
	        
	        
	        	log.info("更新乐观锁故障： {}", ex.getMessage());
	        
	        	return Response.nonExpect(Constant.ErrorCode.ERR_SYSTEM).message("系统故障请稍后再试.");
	        
	       
	        
	    }
	
    @ExceptionHandler(RuntimeException.class)
    @ResponseBody
    Response handleException(RuntimeException ex) {
    	//ex.printStackTrace();
        
        
        if(ex instanceof BusinessException) {
        	log.info(" {}", ex);
        	return Response.nonExpect(((BusinessException) ex).getCode()).message(ex.getMessage());
        }else {
        	log.error(" {}", ex);
        	return Response.nonExpect(Constant.ErrorCode.ERR_GENERAL).message(ex.getMessage());
        }
        
        
    }
    
    
  
    

}
